package reflective;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ReflectiveTest6 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        // 反射常用方法
        Class clazz = String.class;

        // 获取包
        System.out.println(clazz.getPackage().getName());

        // 获取修饰符
        int modifiers = clazz.getModifiers();
        System.out.println("修饰符：" + Modifier.toString(modifiers));

        // 获取全类名
        System.out.println("全类名：" + clazz.getName());

        // 获取父类
        System.out.println("父类：" + clazz.getSuperclass());

        // 获取父接口
        Class[] interfaces = clazz.getInterfaces();
        System.out.print("父接口: ");
        for (Class anInterface : interfaces)
            System.out.print(anInterface.getName() + "\t");
        System.out.println();

        // 操作属性 能得到private  getField只能得到public
        Field field = clazz.getDeclaredField("value");
        System.out.println("value属性：" + field);

        // 获取属性数组
        Field[] declaredFields = clazz.getDeclaredFields();
        System.out.println("属性：");
        for (Field declaredField : declaredFields)
            System.out.println(declaredField + "\t");

        // 获取构造器
        Constructor[] constructors = clazz.getConstructors();
        System.out.println("构造器：");
        for (Constructor constructor : constructors)
            System.out.println(constructor);

        // 获取方法
        Method[] methods = clazz.getDeclaredMethods();
        System.out.println("方法：");
        for (Method method : methods)
            System.out.println(method);

    }
}
